<?php
/**
 * User Groups Create/Update
 * Popup to handle creation and update of user groups
 * 
 * @author Xavier VILAIN
 * @package Hive\User\Group
 * @license #license-hive#
 * 
 */

use Fox\Popup;
use Hive\Ressource\Group;
use Fox\Html\Button;
use Fox\Generator;
use Fox\Html\Screen;

$xview = Xview::instance();

// Authorization Control
if(!Fox\Authorization\Manager::check(Hive\Authorizations::USERS_GROUP_READ)) {
	echo $xview->accessDenied(Msg(Hive::BUNDLE,Hive::ERR_ADMIN_ACCES_DENIED),null,true);
	exit;
}

// Load Group Object
try {
	$group = new Group(Xview::instance()->val('grouping'));
	$data = $group->getData();
	$usersList = array();
	foreach($group->users() as $user) {
		$usersList[$user->idusr] = $user;
		try {
			$userObject = Fox\Dao::load(User::className(),$user->idusr);
			$data['gTarget'][] = $userObject;//$userObject->getData();
		} catch (Fox\Object\Exception\NotExist $e) {
				
		}
	}
}
catch(Fox\Object\Exception\NotExist $e)
{
	Xview::instance()->systemError($e->getMessage(),Generator::url('users','groups'));
	exit;
}

if($group->exists()) {
	$mode = 'update';
	$label = Msg(Hive::BUNDLE,Hive::RESSOURCE_GROUP_UPDATE);
} else {
	$mode = 'create';
	$label = Msg(Hive::BUNDLE,Hive::USER_GROUP_ADD);
}

$screen = new Screen('sGroup',$mode,null,true,Generator::ajaxReload('MC_gGroups'));

// Header Part
$blHeader = Xview::instance()->component('users','groupsHeader');
$screen->add($blHeader);

if($mode == 'update') {
	// Attached Users
	$blUsers = $xview->module(Generator::url('users','groups','users',array('return' => true)));
	$blUsers->item('gTarget')->fieldcat()->switchToObjectMode();
	$blUsers->item('gTarget')->fieldcat()->field('coche')->setGenerator(Generator::useValue(''));
	$screen->add($blUsers);
	if(!$group->active) {
	
		$screen->button(new Button(array('enable','state')))
				->color(Button::COLOR_GREEN);
	} else {
		$screen->button(new Button(array('disable','state')))
				->color(Button::COLOR_RED);
	}
	
}

// Init Screen
$screen->initialize($data);

if($screen->submitted())
{
	$fields = $screen->get();
	if(!$screen->errors())
	{
		try {
			$group->setFields($fields);
			if($group->exists()) {
				
				$group->prepareUpdate(Group::CHANGE_DEFAULT);
				
				$gid = $group->getId();
				foreach($fields['gTarget'] as $item)
				{
					$userGroup = $group->findUsers($gid.':'.$item['idusr']);
					if($userGroup->exists()) {
						unset($usersList[$item['idusr']]);
					} else {
						$userGroup->setFields(array('idusr' => $item['idusr']));
						$group->addToUsers($userGroup);
					}
				}
				if(is_array($usersList))
				{
					foreach($usersList as $user)
					{
						$group->deleteFromUsers($user);
					}
				}
				if($screen->pushed() == 'state') {
					if($group->active) {
						$group->active = false;
					} else {
						$group->active = true;
					}
					$group->update();
					Fox\Message::Success(Msg(Fox\Msg::BUNDLE,Fox\Msg::MSG_UPDATE,$group->name));
				}
				elseif($group->update()) {
					Fox\Message::Success(Msg(Fox\Msg::BUNDLE,Fox\Msg::MSG_UPDATE,$group->name));
				}
				else {
					Fox\Message::Information(Msg(Fox\Msg::BUNDLE,Fox\Msg::MSG_NO_UPDATE,$group->name));
				}
			} else {
				$group->create();
				Fox\Message::Success(Msg(Fox\Msg::BUNDLE,Fox\Msg::MSG_CREATE,$group->name));
			}
			echo $xview->messages(true,true);
			exit;
		}
		catch (Fox\Object\Exception\Save $e) {
			Fox\Message::Error($e->getMessage());
		}
		echo $xview->messages();
		exit;
	}
}
$screen->pageTitle();
$popup = new Popup('pGroup',$label,$screen);
echo $popup->display();